package com.personal_workspace_java.common;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;

/**
* @author 19324
*/
@Data
public class PageRequest {

    //1. 当前页码
    private Integer pageCurrent=1;

    //2. 每页记录数
     private Integer pageSize=10;

    //3. 排序字段名
    private String sortBy;

    //4. 是否升序
    private Boolean isAsc=true;



    public  <T> Page<T> toPage(OrderItem ... items) {
        //1.分页条件
        Page<T> page = Page.of(pageCurrent, pageSize);
            //2.排序条件
            if(sortBy!=null){
                //不为空
                OrderItem orderItem = new OrderItem();
                orderItem.setAsc(isAsc);
                orderItem.setColumn(sortBy);
                page.addOrder(orderItem);
            }else if(items!=null){
            //不为空
                page.addOrder(items);
            }
            return page;
        }

    public  <T> Page<T> toPageDefaultSortByUpdateTime() {
         OrderItem orderItem = new OrderItem();
         orderItem.setAsc(false);
         orderItem.setColumn("update_time");
         return toPage(orderItem);
    }

    public  <T> Page<T> toPage(String sortBy,Boolean isAsc) {
        OrderItem orderItem = new OrderItem();
        orderItem.setAsc(isAsc);
        orderItem.setColumn(sortBy);
        return toPage(orderItem);
    }

    public  <T> Page<T> toPageWithSortBy(String sortBy) {
        OrderItem orderItem = new OrderItem();
        orderItem.setColumn(sortBy);
        return toPage(orderItem);
    }

    public  <T> Page<T> toPageWithIsAsc(Boolean isAsc) {
        OrderItem orderItem = new OrderItem();
        orderItem.setAsc(isAsc);
        return toPage(orderItem);
    }
}
